package com.umeox.iqibla.azkar.ui

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.umeox.iqibla.azkar.R
import com.umeox.iqibla.azkar.adapter.HistoryListAdapter
import com.umeox.iqibla.azkar.db.AppDatabase
import com.umeox.iqibla.azkar.db.Azkar
import com.umeox.iqibla.azkar.utils.DateUtils
import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity
import com.umeox.moto.watch.themes.widget.TextView
import com.umeox.watch.moto.dataservice.DataProvider

/**
 * Created by Rambo 2024/3/28/0028
 * Describe:
 */
class HistoryActivity : ThemeAppCompatActivity(), HistoryListAdapter.ItemClickListener {

    private lateinit var mNoDataTv: TextView
    private lateinit var mContentView: LinearLayout
    private lateinit var mTotalTv: TextView
    private lateinit var mRvHistory: RecyclerView

    private lateinit var historyListAdapter: HistoryListAdapter

    private var lastTotalCount = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_history)
        initView()
        initData()
    }

    private fun initView() {
        mNoDataTv = findViewById(R.id.tv_no_data)
        mContentView = findViewById(R.id.ll_content)
        mTotalTv = findViewById(R.id.tv_total)
        mRvHistory = findViewById(R.id.rv_history)

    }

    private fun initData() {
        historyListAdapter = HistoryListAdapter()
        mRvHistory.adapter = historyListAdapter
        mRvHistory.layoutManager =
            LinearLayoutManager(this, RecyclerView.VERTICAL, false)
        historyListAdapter.itemClickListener = this

        //查询数据库
        val azKarList = AppDatabase.get().azkarDao()
            .loadLast30DayList(DateUtils.getNDaysTimeMillis(-7), DateUtils.getTodayMillis())
        if (azKarList?.isNotEmpty() == true) {
            mNoDataTv.visibility = View.GONE
            mContentView.visibility = View.VISIBLE
            val totalCount = AppDatabase.get().azkarDao().queryLast30DayListSumCount(
                DateUtils.getNDaysTimeMillis(-7),
                DateUtils.getTodayMillis()
            )
            lastTotalCount = totalCount
            mTotalTv.text =
                if (DataProvider.isActivated()) "${DataProvider.getChantsCounterTotal()}" else "${totalCount}"
            historyListAdapter.setDataList(azKarList)
        } else {
            mNoDataTv.visibility = View.VISIBLE
            mContentView.visibility = View.GONE
        }
    }

    override fun onItemClicked(azkar: Azkar) {
    }


}
